package cn.yunhe.controller;

import cn.yunhe.beans.Item;
import cn.yunhe.beans.Order;
import cn.yunhe.service.ItemService;
import cn.yunhe.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import java.util.UUID;

/**
 * @author herbert
 * @date 2021/2/6
 */
@Controller
@RequestMapping("/order")
public class OrderController {


    @Autowired
    OrderService orderService;

    /**
     * 查询订单列表
     * @param modelMap
     * @return
     */
    @RequestMapping("/queryList.do")
    public String queryUsers(ModelMap modelMap) {
        List<Order> orderList = orderService.queryOrderList();
        modelMap.addAttribute("orderList", orderList);
        return "order-list";
    }

    /**
     * 添加产品
     *
     * @param order
     * @param request
     * @return
     * @throws IOException
     * @throws IOException
     * @throws ParseException
     */
    @RequestMapping("/insertOrder.do")
    public String insertItem(@RequestBody Order order, HttpServletRequest request) throws IOException, IOException, ParseException {
       orderService.insertOrder(order);

        return "redirect:/order/queryList.do";
    }


    /**
     * 根据id查询产品
     *
     * @param modelMap
     * @param id
     * @return
     */
    @RequestMapping("/queryOrderById.do")
    public String queryOrderById(ModelMap modelMap, Integer id) {
//         * 原生的写法应该是以HttpServletRequest request
//         * String id = request.getParameter("id");
        Order order = orderService.queryOrderById(id);
        modelMap.addAttribute("order", order);
        return "order-edit";
    }

    /**
     * 更新用户信息
     * @param order
     * @return
     */
    @RequestMapping("/updateOrder.do")
    public String updateOrder(@RequestBody Order order,HttpServletRequest request) throws IOException, IOException, ParseException {
        orderService.updateOrder(order);
        return "redirect:/order/queryList.do";
    }


    /**
     * 删除个别用户
     *
     * @param orderId
     * @param response
     * @param request
     * @return
     */
    @RequestMapping("/deleteOrder.do")
    public String deleteOrder(Integer orderId, HttpServletResponse response, HttpServletRequest request) {
        orderService.deleteOrder(orderId);
        return "redirect:/order/queryList.do";
    }

    /**
     * 多项删除
     *
     * @param orderIds
     * @param response
     * @param request
     * @return
     */
    @RequestMapping("/deleteAll.do")
    public String deleteAll(Integer[] orderIds, HttpServletResponse response, HttpServletRequest request) {
        orderService.deleteAll(orderIds);
        return "redirect:/order/queryList.do";
    }


}
